package acitivity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import java.util.List;

import Entity.LoginStateMedel;
import util.UtilX;
import vm.LoginModel;

/**
 * The type Login activity.
 */
public class LoginActivity extends BaseActivityX {

    private LoginModel mViewModel;

    @Override
    public boolean showSystemWindows() {
        return false;
    }

    /**
     *
     */
    @Override
    public void init() {

        mViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(LoginModel.class);

        //用户名
        mViewModel.getNameEvent().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s)
            {
               // ToastUtils.showLong(s);
            }
        });
        //密码
        mViewModel.getmPwEvent().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {

            }
        });

        mViewModel.getLoginState().observe(this, new Observer<LoginStateMedel>() {
            @Override
            public void onChanged(LoginStateMedel stateMedel) {
                        //正在登录
                    if(stateMedel.getState()==1){

                        //登录失败
                    }else if(stateMedel.getState()==2){

                        //登录成功
                    }else if(stateMedel.getState()==3){
                    Intent intent=new Intent(LoginActivity.this, WeatherActivity.class);
                    startActivity(intent);
                    }
            }
        });


        activityMainBinding.activityLoginSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewModel.login(activityMainBinding.activityLoginUser.getText().toString(),
                        activityMainBinding.activityLoginPs.getText().toString()
                ,activityMainBinding.activityLoginPrivacy.isChecked());
            }
        });

        activityMainBinding.activityLoginUser.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    mViewModel.getNameEvent().setValue(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        activityMainBinding.tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, YS_Activity.class);
                startActivity(intent);
            }
        });

        activityMainBinding.activityLoginPs.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mViewModel.getNameEvent().setValue(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
//        if(RomUtils.isHuawei()){
//            getToken();
//        }else if(RomUtils.isXiaomi()){
//            XiaomiPushUtils.registerPushXiaomi(this);
//        }

    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    private void xiaomiPushRegist(){

        //打开Log

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean shouldInit() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getApplicationInfo().processName;
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取白名单
     */



    /**
     * 华为推送获取token
     */


    private void sendRegTokenToServer(String token) {
        UtilX.LogX("sending token to server. token:" + token);
    }

}
